////////////////////////////////////////////////
// DESCRIPTION:
//    Sample I2C Device Driver
//
// Legal Notices:
//   Copyright (c) 2008, Telliam Consulting, LLC.
//   All rights reserved.
//
//   Redistribution and use in source and binary forms, with or without modification,
//   are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice, this list
//     of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above copyright notice, this
//     list of conditions and the following disclaimer in the documentation and/or other
//     materials provided with the distribution.
//   * Neither the name of Telliam Consulting nor the names of its contributors may be
//     used to endorse or promote products derived from this software without specific
//     prior written permission.
//
//   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
//   EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
//   SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
//   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
//   BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
//   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
//   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
//   DAMAGE. 
//
using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

namespace FusionWare.SPOT.Hardware
{
    /// <summary>Managed Driver for the emulated Simple I2CDevice</summary>
    /// <remarks>
    /// This demonstrates a managed code driver for an I2C Device. The device
    /// is the emulated <see cref="FusionWare.Emulator.SimpleI2CDevice" />.
    /// It has only one 16 bit "Chip ID" register (or command/response if you like).
    /// 
    /// While the device communication and "register set" is simplistic this
    /// sample is a complete driver illustrating best practices for managed drivers.
    /// This includes encapsulating internal details, isolating the device driver from
    /// the bus ther device is on and supporting IDisposable so that GPIO Pin
    /// resources are properly freed by deriving from <see cref="I2CDeviceDriver" />. 
    /// This may seem like an academic excercise, however it is vitally important for
    /// re-usablity of drivers in different systems. It is possible that a system could
    /// share the GPIO Pins for the underlying bus via an external multiplexer. If
    /// proper encapsulation and IDisposable are not used correctly then the driver
    /// would not work on such a system. 
    /// </remarks>
    partial class SimpleI2CDevice : I2CDeviceDriver
    {
        const int ClockRateKhz = 25;

        /// <summary>Creates a new device instance</summary>
        /// <param name="Address">Address of the bus (including read write bit of 0)</param>
        /// <remarks>
        /// This will create a new device instance for a SimpleI2CDevice at the specified address.
        /// The address is the same as used by the I2CDevice.Configuration. 
        /// </remarks>
        public SimpleI2CDevice(I2CBus Bus, ushort Address)
            : base(Bus, Address, ClockRateKhz)
        {
        }
	
        /// <summary>Gets the Chip ID</summary>
        /// <value>Chip ID retrieved from the device</value>
        /// <remarks>
        /// This property will cache the ID so that it is actually read from the
        /// device only once. This is a read only register.
        /// </remarks>
        /// <Note>This is just a sample of a possible register. No known real device actually implements this particular functionality</Note>
        public ChipIDReg ChipID
        {
            get
            {
                // 0 is the initial state of the value
                // it is also an invalid ID so use that
                // to check if the data is cached already.
                if(this._ChipID.Value == 0)
                    this._ChipID.Value = base.ReadReg16( (byte)Registers.ChipID, ByteOrder.LittleEndian);

                return this._ChipID;
            }
        }
        private ChipIDReg _ChipID;

        public byte Scratch8
        {
            get { return base.ReadReg8((byte)Registers.Scratch8); }
            set { base.WriteReg8((byte)Registers.Scratch8, value); }
        }

        public ushort Scratch16
        {
            get { return base.ReadReg16((byte)Registers.Scratch16, ByteOrder.LittleEndian); }
            set { base.WriteReg16((byte)Registers.Scratch16, value, ByteOrder.LittleEndian); }
        }
   }
}
